package com.sunmouse.toolkit.util;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;

import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.constant.Material;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;

/**
 * Created by Administrator on 2017/9/11 0011.
 */
public class ChartUtil {
    private static ChartUtil mUtil;
    private Line mLine;
    private Axis mAxis;
    private Context context;

    /**
     *
     */
    private ChartUtil(Context context) {
        this.context = context;
    }

    /**
     * single instance
     *
     * @return
     */
    public static ChartUtil getInstance(Context context) {
        if (mUtil == null) {
            mUtil = new ChartUtil(context);
        }
        return mUtil;
    }

    public static ChartUtil newInstance(Context context) {
        return new ChartUtil(context);
    }

    /**
     * generate a line
     */
    public Line generateLine(Float... floats) {
        ArrayList<PointValue> points = new ArrayList<>();
        if (floats.length % 2 == 0) {
            for (int i = 0; i < floats.length / 2; i++) {
                points.add(new PointValue(floats[2 * i], floats[2 * i + 1]));
            }
        } else {
            for (int i = 0; i < floats.length / 2; i++) {
                points.add(new PointValue(floats[2 * i], floats[2 * i + 1]));
            }
            points.add(new PointValue(floats[floats.length - 1], 0));
        }
        mLine = new Line(points)
                .setColor(R.color.colorAccent)
                .setSquare(true);

        return mLine;
    }

    /**
     * generate Axis
     */
    public Axis generateAxis(String axisName, @ColorRes int lineColor, @ColorRes int txtColor) {
        Axis xAxis = new Axis();//set coordination
        xAxis.setName(axisName);
        xAxis.setAutoGenerated(true);//auto generate coordination
        xAxis.setTextColor(context.getResources().getColor(txtColor));
        if (lineColor > 0) {
            xAxis.setLineColor(context.getResources().getColor(lineColor));
            xAxis.setHasLines(true);//multi line mode
        }
        xAxis.setTypeface(Typeface.DEFAULT_BOLD);
//        xAxis.setValues();//set x axis values and flag name
//        xAxis.setInside(true);
//        xAxis.setHasSeparationLine(true);
        return xAxis;
    }

    /**
     * generate line chart mData
     */
    public LineChartData generateLineChartData(List<Line> lines, Axis xAxis, Axis yAxis) {
        LineChartData mData = new LineChartData();
        mData.setLines(lines);
        mData.setAxisXBottom(xAxis);
        mData.setAxisYLeft(yAxis);

        return mData;

    }


}
